package cn.com.scau.tstjxt.util;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class SpringUtil implements ApplicationContextAware{
	private static final String[] CONTEXT_PATHS = {"classpath:applicationContext.xml"};
	private static ApplicationContext ctx;

	/**
	 * just for Spring init web application context
	 */
	public void setApplicationContext(ApplicationContext webApplicationContext)
			throws BeansException {
		ctx = webApplicationContext;
	}

	public static ApplicationContext getApplicationContext() {
		if(GlobalUtil.appContext==null&&ctx==null){
			ctx = new ClassPathXmlApplicationContext("applicationContext*.xml");   
		}else if (null == ctx) {
			ctx = WebApplicationContextUtils.getWebApplicationContext(GlobalUtil.appContext);
			if(null==ctx){
				ctx = new ClassPathXmlApplicationContext(
						CONTEXT_PATHS);
			}
			
		}
		return ctx;
	}

	public static Object getBean(String name) {
		return getApplicationContext().getBean(name);
	}

}
